package com.freesky.mobile.report.controller;

import cn.hutool.core.date.DateUtil;
import com.freesky.biz.activity.service.ICouponBillService;
import com.freesky.biz.report.domain.TReportDayGrant;
import com.freesky.biz.report.domain.TReportDayStock;
import com.freesky.biz.report.domain.TReportProject;
import com.freesky.biz.report.service.IReportDayStockService;
import com.freesky.common.core.controller.BaseController;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.domain.entity.SysRole;
import com.freesky.common.core.domain.entity.SysUser;
import com.freesky.common.core.page.TableDataInfo;
import com.freesky.common.utils.SecurityUtils;
import com.freesky.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mobile/report")
public class ReportController extends BaseController {

    @Autowired
    private IReportDayStockService reportDayStockService;

    @Autowired
    private ICouponBillService couponBillService;

    @GetMapping("/projectList")
    public TableDataInfo projectList(TReportProject entity) {
        startPage();
        getDataScore(entity.getParams());
        List<TReportProject> list = reportDayStockService.selectReportProjectList(entity);
        return getDataTable(list);
    }

    @GetMapping("/summary")
    public AjaxResult summary(TReportProject entity) {
        getDataScore(entity.getParams());
        TReportProject reportProject = reportDayStockService.selectSummaryReportProject(entity);
        if(reportProject!=null){
            String lastMaxDate = couponBillService.selectMaxUpdateDate();
            if(StringUtils.isNotEmpty(lastMaxDate)){
                reportProject.setDataTime(lastMaxDate.substring(0,10));
            }
        }
        return AjaxResult.success(reportProject);
    }


    @GetMapping("/reportByDayList")
    public AjaxResult reportByDayList(TReportDayStock entity) {
        return AjaxResult.success(reportDayStockService.selectReportByDayList(entity));
    }

    @GetMapping("/reportList")
    public TableDataInfo reportList(TReportDayStock entity) {
        startPage();
        List<TReportDayStock> list = reportDayStockService.selectReportByDayList(entity);
        return getDataTable(list);
    }

    //（1：全部数据权限 2：自定数据权限 3：本部门数据权限 4：本部门及以下数据权限）
    //发放记录
    @GetMapping("/reportGrantList")
    public TableDataInfo reportList(TReportDayGrant entity) {
        startPage();
        List<TReportDayGrant> list = reportDayStockService.selectGrantDayList(entity);
        return getDataTable(list);
    }

    @GetMapping("/daySummary")
    public AjaxResult daySummary(TReportDayStock entity) {
        return AjaxResult.success(reportDayStockService.selectReportDaySummary(entity));
    }


    private void getDataScore(Map<String, Object> params){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        List<SysRole> sysRoleList = sysUser.getRoles();
        //数据范围（1：全部数据权限 2：自定数据权限 3：本部门数据权限 4：本部门及以下数据权限,5:本人数据
        int nDataScope = 5;
        Long roleId = 0L;
        for(SysRole role : sysRoleList){
            if(role.isAdmin()){
                nDataScope = 1;
                break;
            }
            int nCurDataScope = Integer.parseInt(role.getDataScope());
            if(nCurDataScope < nDataScope){
                nDataScope = nCurDataScope;
                roleId = role.getRoleId();
            }
        }

        if(nDataScope == 1){
            params.put("sql","");
        } else if(nDataScope == 2) {
            params.put("sql"," AND b.dept_id in (select dept_id from sys_role_dept where role_id = "+roleId +")");
        } else if(nDataScope==3){
            params.put("sql"," AND b.dept_id = " + sysUser.getDeptId());
        } else if(nDataScope == 4){
            params.put("sql"," AND ( b.dept_id = " + sysUser.getDeptId()+" OR FIND_IN_SET("+sysUser.getDeptId()+",b.ancestors))" );
        } else if(nDataScope==5){
            params.put("sql"," AND a.link_mobile = '" + sysUser.getPhonenumber() +"'" );
        }

        logger.info(sysUser.getUserName()+":"+ params.get("sql"));
    }

}
